//package com.itheima.backend.service;
//
//import com.itheima.backend.mapper.MessageMapper;
//import com.itheima.backend.pojo.Message;
//import com.itheima.backend.service.JIEKOU.MessageService;
//import org.springframework.stereotype.Service;
//
//import java.util.List;
//
//@Service
//public class MessageServiceImpl implements MessageService {
//
//    private final MessageMapper messageMapper;
//
//    public MessageServiceImpl(MessageMapper messageMapper) {
//        this.messageMapper = messageMapper;
//    }
//
//    @Override
//    public void createMessage(Message message) {
//        messageMapper.insertMessage(message);
//    }
//
//    @Override
//    public void deleteMessageById(Integer id) {
//        messageMapper.deleteMessageById(id);
//    }
//
//    @Override
//    public List<Message> getMessages(int page, int size) {
//        int offset = (page - 1) * size;
//        return messageMapper.selectMessages(offset, size);
//    }
//
//    @Override
//    public List<Message> searchMessages(String keyword, Integer userId) {
//        System.out.println("Keyword: " + keyword + ", UserId: " + userId);  // 打印参数
//        return messageMapper.searchMessages(keyword, userId);
//    }
//}
package com.itheima.backend.service;

import com.itheima.backend.mapper.MessageMapper;
import com.itheima.backend.pojo.Message;
import com.itheima.backend.service.JIEKOU.MessageService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MessageServiceImpl implements MessageService {

    private final MessageMapper messageMapper;

    public MessageServiceImpl(MessageMapper messageMapper) {
        this.messageMapper = messageMapper;
    }

    @Override
    public void createMessage(Message message) {
        messageMapper.insertMessage(message);
    }

    @Override
    public void deleteMessageById(Integer id) {
        messageMapper.deleteMessageById(id);
    }

    @Override
    public List<Message> getMessages(int page, int size) {
        int offset = (page - 1) * size;
        return messageMapper.selectMessages(offset, size);
    }

    @Override
    public List<Message> searchMessages(String keyword, String username,int page) {
        System.out.println("Keyword: " + keyword + ", UserName: " + username + ", Page: " + page + ", Size: " + 5);

        int offset = (page - 1) * 5; // 计算偏移量
        return messageMapper.searchMessages(keyword, username, offset, 5);
    }

    // 新增方法：根据用户 ID 分页查询留言
    @Override
    public List<Message> getMessagesByUserId(Integer userId, int page, int size) {
        int offset = (page - 1) * size;
        return messageMapper.selectMessagesByUserId(userId, offset, size);
    }

    // 新增方法：根据留言 ID 获取单条留言
    @Override
    public Message getMessageById(Integer id) {
        return messageMapper.selectMessageById(id);
    }


    public List<Message> searchAllmessages(String keyword,int page) {
        int offset = (page - 1) * 5; // 计算偏移量
        return messageMapper.selectAllmessages(keyword,offset,5);
    }


}
